package jk.rule.core.match;

import jk.rule.core.Rule;
import org.apache.commons.lang3.ClassUtils;

import java.lang.instrument.IllegalClassFormatException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author cuichao
 * 类型匹配器
 */
public class TypeMatcher implements RuleMatcher{
    @Override
    public boolean match(Rule rule) {
        Class<?> type = getType(rule.getClass());
        System.out.println(type);
        return false;
    }


    private Class<?> getType(Class<? extends Rule> ruleClass){
        ParameterizedType type = (ParameterizedType) ruleClass.getGenericInterfaces()[0];
        Type actualType = type.getActualTypeArguments()[0];
        return checkType(ruleClass);
    }


    private Class<?> checkType(Type type){
        if(type instanceof Class<?>){
           return  (Class<?>) type;
        }else if (type instanceof  ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type t = parameterizedType.getActualTypeArguments()[0];
            return  checkType(t);
        }else {
            String className = type == null ? "null" : type.getClass().getName();
            throw new IllegalArgumentException("Expected a class, ParameterizedType, but <"+ type + "> is of type "+ className);
        }
    }
}
